package fejezet20;

import java.awt.*;
import javax.swing.JApplet;

class Pont {
  public double x, y;

  public Pont(double x, double y) {
    this.x=x;
    this.y=y;
  }
}

public class BarnsleyPáfrány extends JApplet {
  @Override
  public void start() {
    setSize(350, 350);
  }

  @Override
  public void paint(Graphics g) {
    final int N=10000, skála=30;
    g.setColor(Color.GREEN);
    Pont origó=new Pont(150, 350);
    Pont pont=new Pont(0, 0);
    double p;
    for (int i=1; i<=N; i++) {
      p=Math.random()*100+1; //1<=p<=100
      if(p==1)               //1%
        pont=f1(pont);
      else if(p<=86)        //85%
        pont=f2(pont);
      else if(p<=93)         //7%
        pont=f3(pont);
      else                   //7%
        pont=f4(pont);
      g.drawOval((int)(origó.x+pont.x*skála),
                 (int)(origó.y-pont.y*skála), 1, 1);
    }
  }

  private Pont f1(Pont pont) {
    return new Pont(0,
                    0.16*pont.y);
  }

  private Pont f2(Pont pont) {
    return new Pont(0.85*pont.x+0.04*pont.y,
                    -0.04*pont.x+0.85*pont.y+1.6);
  }

  private Pont f3(Pont pont) {
    return new Pont(0.2*pont.x-0.26*pont.y,
                    0.23*pont.x+0.22*pont.y+1.6);
  }

  private Pont f4(Pont pont) {
    return new Pont(-0.15*pont.x+0.28*pont.y,
                    0.26*pont.x+0.24*pont.y+0.44);
  }
}